/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package InternetHax;

import java.util.Vector;

/**
 * The game manager is responsable for handling the order of turns and other turn-related logic
 * such as tile effects. It keeps a queue of actors and iterates through each of them. It will eventually become the "game loop". 
 * @author LoginError
 */
public class GameManager {

    private Vector allTheActors;
    int currentTurn = 0;
    int roundCounter;
     public GameManager() {
        allTheActors = new Vector();
    }

    /**
     * checks to see if the current actor taking a turn is the player;
     * @return true if the current actor is the player, false otherwise
     */
    synchronized public boolean isPlayerTurn() {
        return getCurrentTurn().isHero();
    }

    /**
     * Takes all the turns until it's the hero's turn, then returns
     * NOTE: this actually calls the hero's takeTurn() method, although it shouldn't
     * do anything
     */
    synchronized public void runUntilHeroTurn() {
        
        do {
            Actor aMonster = getNextTurn();
            aMonster.takeTurn();

        } while (!isPlayerTurn());

    }


     public void reset()
     {
         currentTurn = 0;
         allTheActors.removeAllElements();
     }


    /**
     * Gets the next actor in line to take a turn, wrapping around to the start of the list if nessicary
     * @return the actor next in line to take a turn, or null in the event of an error
     */
    synchronized public Actor getNextTurn() {
        
        currentTurn = ++currentTurn % allTheActors.size();// yay MOD math :)
        if(currentTurn == 0)
        {   
            roundCounter++;
            Toolbox.printDebug("\nGamemanager: \"Round "+roundCounter+" is over\"\n");
        }
        return (Actor) allTheActors.elementAt(currentTurn);
    }

    /**
     * returns the actor currently taking a turn
     * @return the actor currently taking a turn, or null in the event of an error
     */
    synchronized public Actor getCurrentTurn() {

        return (Actor) allTheActors.elementAt(currentTurn);
    }
     
     
     

    /**
     * Returns the actor that took their turn previously to the current actor
     * @return the previous actor, or null in the event of an error
     */
    synchronized public Actor getLastTurn() {
        currentTurn -= 1;
        if (currentTurn < 0) {
            currentTurn = allTheActors.size() - 1;
        }
        return (Actor) allTheActors.elementAt(currentTurn);
    }

    synchronized public int getTurnIndex() {
        return currentTurn;
    }

    synchronized public void setTurnIndex(int currentTurn) {
        this.currentTurn = currentTurn;
    }

    /**
     * checks to see if a speicified actor has taken their turn this round
     * @param actor the actor to check
     * @return returns true if the actor has taken their turn, false if not, or on error
     */
    synchronized public boolean hasTurnPassedThisRound(Actor actor) {
        //if the position of the actor is greater than the current turn then it's turn hasn't come yet
        int passed = allTheActors.indexOf(actor, currentTurn);

        //not found from current to end, so must have passed
        if (passed == -1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Adds a new actor to the end of the turn queue
     * @param actor the actor to add
     */
    synchronized public void addNewActor(Actor actor) {
        if (allTheActors.isEmpty()) {
            allTheActors.addElement(actor);
        } else {
            allTheActors.insertElementAt(actor, allTheActors.size() - 1);
        }
    }
     
    /**
     * removes a specified actor from the turn list
     * @param actor the actor to remove
     */
    synchronized public void removeActor(Actor actor) {
        allTheActors.removeElement(actor);
    }

    /**
     * @return the allTheActors
     */
    synchronized  public Vector getAllTheActors() {
        return allTheActors;
    }

    /**
     * @param allTheActors the allTheActors to set
     */
    synchronized public void setAllTheActors(Vector allTheActors) {
        this.allTheActors = allTheActors;
    }
}

